package mya.mes.mdc.logic.mqtt;

import mya.mes.mdc.mongodb.Mongo;
import mya.mes.mdc.share.plc.PlcTagVoProcessor;
import mya.mes.mdc.share.plc.TagPrefix;
import mya.mes.mdc.share.vo.plc.PlcTagVo;
import org.bson.Document;

import java.util.Iterator;
import java.util.List;

public class DataSaveToMongoProcessor implements PlcTagVoProcessor<Void> {

    @Override
    public String getId() {
        return "DATA_SAVE_TO_MONGO_DEFAULT";
    }

    @Override
    public Void process(Long mqttMsgId, List<PlcTagVo> voList) throws Exception {
        boolean result = false;
        Iterator<PlcTagVo> iter = voList.iterator();
        while (iter.hasNext()) {
            PlcTagVo item = iter.next();
            if ("SAVE".equals(item.dataType)) {
                iter.remove();
                result = true;
                break;
            }
        }
        if (result) {
            saveMdcDataToMongodb(mqttMsgId, voList);
        }
        return null;
    }

    public Void process(Long mqttMsgId, List<PlcTagVo> voList, String collectionName) throws Exception {
        boolean result = false;
        Iterator<PlcTagVo> iter = voList.iterator();
        while (iter.hasNext()) {
            PlcTagVo item = iter.next();
            if ("SAVE".equals(item.dataType)) {
                iter.remove();
                result = true;
                break;
            }
        }
        if (result) {
            saveDataToMongodb(mqttMsgId, voList, collectionName);
        }
        return null;
    }

    private void saveMdcDataToMongodb(Long mqttMsgId, List<PlcTagVo> voList) throws Exception {
        if (voList == null || voList.isEmpty()) {
            return;
        }
        Document doc = buildDoc(mqttMsgId, voList);
        if (doc == null) {
            return;
        }
        Mongo.getInstance().getMongoDbService().saveMdcData(doc);
    }

    private void saveDataToMongodb(Long mqttMsgId, List<PlcTagVo> voList, String collectionName) throws Exception {
        if (voList == null || voList.isEmpty()) {
            return;
        }
        Document doc = buildDoc(mqttMsgId, voList);
        if (doc == null) {
            return;
        }
        Mongo.getInstance().getMongoDbService().saveData(doc, collectionName);
    }


    public Document buildDoc(Long mqttMsgId, List<PlcTagVo> data) {

        if (data == null || data.isEmpty()) {
            return null;
        }
        Document doc = new Document();
        doc.put("_id", mqttMsgId);
        for (PlcTagVo vo : data) {
            if (TagPrefix.isNumber(vo.dataType)) {
                doc.put(vo.tagName, vo.tagIntValue);
                continue;
            }
            if (TagPrefix.isString(vo.dataType)) {
                doc.put(vo.tagName, vo.tagStrValue);
                continue;
            }
            if (TagPrefix.isBoolean(vo.dataType)) {
                doc.put(vo.tagName, vo.tagBoolValue);
                continue;
            }
        }

        return doc;
    }


}
